package com.xingkeduo.dto.transter.marketingsuccessdetail;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.utils.DateUtils;

import lombok.Data;

@Data
public class MarketingSuccessDetail {
	private String id;// 券，卡ID
	private String marketing_type;// 类型
	private String title;// 券，卡名称
	private List<Marketing_success_detail> coupon_bag;
	private List<Marketing_success_detail> card_bag;
	private String store_id;
	private String store_name;
	private String use_end_time;
	private String card_type;
    private List<String> reduce_cost;
    private List<String> discount;
    private String least_cost;
    private String use_memo;

	public void covert(JSONObject json) {
		this.id = json.getOrDefault("id", "") == null ? "" : json.getOrDefault("id", "").toString();
		this.title = json.getOrDefault("name", "") == null ? "" : json.getOrDefault("name", "").toString();
		this.marketing_type = json.getOrDefault("marketingType", "") == null ? ""
				: json.getOrDefault("marketingType", "").toString();
		String storeIds = json.getOrDefault("storeIds", "0") == null ? "0"
				: json.getOrDefault("storeIds", "0").toString();
		this.use_memo=json.getOrDefault("useMomo", "")==null?"":json.getOrDefault("useMomo", "").toString();
		if(StringUtils.isNotBlank(storeIds)) {
			if(storeIds.startsWith("0,")) {
				storeIds = storeIds.substring(2, storeIds.length());
			}
			if(storeIds.endsWith(",0")) {
				storeIds = storeIds.substring(0, storeIds.length()-2);
			}
		}
		this.store_id = storeIds;
		this.store_name="适用部分门店";
		if("0".equals(store_id)) {
			this.store_name="适用所有门店";
		}
//		this.store_name = json.getOrDefault("storeNames", "0") == null ? "0"
//				: json.getOrDefault("storeNames", "0").toString();
		String userType = json.getOrDefault("useType", "0") == null ? "0"
				: json.getOrDefault("useType", "0").toString();
		if ("0".equals(userType)) {
			Integer usePeriod = json.getOrDefault("usePeriod", "0") == null ? 0
					: Integer.parseInt(json.getOrDefault("usePeriod", "0").toString());
			Date startDate = DateUtils.getCurdate();
			Date endDate = DateUtils.addSeconds(DateUtils.addSomeDays(startDate, usePeriod),-1);
			this.use_end_time = endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT);
		} else {
			Date endDate = json.getDate("useEndTime");
			this.use_end_time = endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT);
		}
		BigDecimal leastAmount = new BigDecimal(json.getOrDefault("leastCost", "0")==null?"0":json.getOrDefault("leastCost", "0").toString());
		leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
		this.least_cost = leastAmount.toString();
		String couponAmount = json.getOrDefault("couponAmount", "0") == null ? "0"
				: json.getOrDefault("couponAmount", "0").toString();
		this.card_type = json.getOrDefault("serviceType", "") == null ? ""
				: json.getOrDefault("serviceType", "").toString();
		if (StringUtils.isBlank(couponAmount)) {
			this.reduce_cost = Lists.newArrayList("0", "0");
		} else {
			BigDecimal amount = new BigDecimal(couponAmount);
			if("DISCOUNT".equalsIgnoreCase(card_type)) {
				amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
				this.discount = Lists.newArrayList(one+"",two+"");
			}else{
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
				two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
				this.reduce_cost = Lists.newArrayList(one+"",(two+"").replaceAll("\\.", ""));
			}
		}
	}
}
